package com.longrise.common.utils;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.longrise.common.R;

import org.apache.http.conn.util.InetAddressUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by callxkj on 2017/9/22.
 * 关于网络的工具类
 */
public class NetUtil {

    /**
     * 检查网络是否可用 并弹toast
     */
    public static boolean checkNetEnable() {
        ConnectivityManager connectivity = (ConnectivityManager) AppUtil.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] infos = connectivity.getAllNetworkInfo();
            if (infos != null) {
                for (NetworkInfo networkInfo : infos) {
                    if (networkInfo == null) {
                        continue;
                    }
                    if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        DZZWTools.showToast(AppUtil.getContext(), AppUtil.getContext().getString(R.string.nonetwork), Toast.LENGTH_SHORT);
        return false;
    }


    /**
     * GPS是否开启
     */

    public static boolean isGPSEnabled(Context cxt) {
        if (cxt != null) {
            LocationManager locationManager = ((LocationManager) cxt.getSystemService(Context.LOCATION_SERVICE));
            List<String> accessibleProviders = locationManager.getProviders(true);
            return accessibleProviders != null && accessibleProviders.size() > 0;
        }
        return false;
    }

    /**
     * wifi是否开启
     */
    public static boolean isWifiEnabled(Context context) {
        if (context != null) {
            ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            TelephonyManager mgrTel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return ((mgrConn.getActiveNetworkInfo() != null && mgrConn.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
        }
        return false;
    }

    /**
     * 判断当前网络类型：wifi网络
     */
    public static boolean isWifi(Context cxt) {
        if (cxt != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) cxt.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断当前网络类型：数据网络
     */
    public static boolean isDataNet() {
        ConnectivityManager connectivityManager = (ConnectivityManager) AppUtil.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
        return false;
    }

    /**
     * 当前是：WIFI 或者 3G
     *
     * @return "3G" "WIFI"
     */
    public static String isNetWorkType(Context cxt) {
        if (cxt != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) cxt.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                return "3G";
            }
            if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return "WIFI";
            }
        }
        return "";
    }

    //xkj
    public static String getLocalHostIp() {
        String ipaddress = "";
        try {
            Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces();
            // 遍历所用的网络接口
            while (en.hasMoreElements()) {
                NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
                Enumeration<InetAddress> inet = nif.getInetAddresses();
                // 遍历每一个接口绑定的所有ip
                while (inet.hasMoreElements()) {
                    InetAddress ip = inet.nextElement();
                    if (!ip.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(ip
                            .getHostAddress())) {
                        return ipaddress = ip.getHostAddress();
                    }
                }

            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return ipaddress;

    }
}
